<?php
namespace Home\Controller;

use Think\Controller;
header("Content-type:text/html;Charset=utf-8");
class AdminController extends Controller
{
    /*管理员登录*/
    function login()
    {
        if(isset($_GET['quit'])){
            cookie('tk',null);
            $this->redirect('Admin/login','',1,'您已退出');
        }
        $this->display();
    }

    /*管理员添加文章*/
    function article()
    {
        if(isset($_COOKIE['tk'])){
            $tk=$_COOKIE['tk'];
            if(isset($_SESSION[$tk])){
                $name=$_SESSION[$tk];
                $this->assign('name',$name);
                $this->display();
            }else{
                $this->redirect('Admin/login','',1,'未登录');
            }
        }else{
            $this->redirect('Admin/login','',1,'未登录');
        }
    }

    /*用户文章管理*/
    function myuser()
    {
        if(isset($_COOKIE['tk'])){
            $tk=$_COOKIE['tk'];
            if(isset($_SESSION[$tk])){
                $name=$_SESSION[$tk];
                $this->assign('name',$name);
                $myuser=M('user_addarticle');
                $ulist=$myuser->select();
                $this->assign('ulist',$ulist);
                $this->display();
            }else{
                $this->redirect('Admin/login','',1,'未登录');
            }
        }else{
            $this->redirect('Admin/login','',1,'未登录');
        }
    }

    /*查看用户当前文章*/
    function userarticle(){
        if(isset($_COOKIE['tk'])){
            $tk=$_COOKIE['tk'];
            if(isset($_SESSION[$tk])){
                $name=$_SESSION[$tk];
                $this->assign('name',$name);
                if(isset($_GET['id'])){
                    $id=$_GET['id'];
                    $uart=M('user_addarticle');
                    $art=$uart->where("id=$id")->select();
                    $this->assign('art',$art);
                    $this->display();
                }
            }
        }else{
            $this->redirect('Admin/login','',1,'未登录');
        }

    }

    /*显示用户提交的文章*/
    function showarticle()
    {
        if(isset($_COOKIE['tk'])){
            $tk=$_COOKIE['tk'];
            if(isset($_SESSION[$tk])){
                $name=$_SESSION[$tk];
                $this->assign('name',$name);
                $uarticle=M('user_addarticle');
                $artlist=$uarticle->select();
                $this->assign('artlist',$artlist);
                $User = M('user_addarticle'); // 实例化User对象
                $count  = $User->count();// 查询满足要求的总记录数
                $Page  = new \Think\Page($count,4);// 实例化分页类 传入总记录数和每页显示的记录数(25)
                $show  = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
                $list = $User->limit($Page->firstRow.','.$Page->listRows)->select();
                $this->assign('list',$list);// 赋值数据集
                $this->assign('page',$show);// 赋值分页输出
                $this->display(); // 输出模板
            }else{
                $this->redirect('Admin/login','',1,'未登录');
            }
        }else{
            $this->redirect('Admin/login','',1,'未登录');
        }
    }
}